package com.weisy.signin.ui.fragment;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.squareup.picasso.Picasso;
import com.weisy.signin.BuildConfig;
import com.weisy.signin.R;
import com.weisy.signin.base.BaseFragment;
import com.weisy.signin.bean.Student;
import com.weisy.signin.bean.Teacher;
import com.weisy.signin.comm.Comm;
import com.weisy.signin.utils.GsonUtils;
import com.weisy.signin.utils.ImageUtils;
import com.weisy.signin.utils.ShareUtils;
import com.weisy.signin.utils.SpUtils;
import com.weisy.signin.widget.CircleTransform;
import com.weisy.signin.widget.MsgDialog;

import java.net.URI;
import java.util.Objects;

import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author weisy
 * @function 我的
 * @date 2019/3/21 21:22
 */
public class MineFragment extends BaseFragment {
    @BindView(R.id.img_mine_head)
    ImageView imgHead;

    @BindView(R.id.txt_mine_name)
    TextView txtName;
    @BindView(R.id.txt_mine_id)
    TextView txtId;
    @BindView(R.id.txt_mine_major)
    TextView txtMajor;

    @BindView(R.id.txt_hint_id)
    TextView txtHintId;
    @BindView(R.id.txt_hint_name)
    TextView txtHintName;

    private ShareUtils shareUtils;
    private int userType;
    private Teacher teacher;
    private Student student;

    @Override
    public int getLayoutId() {
        return R.layout.frag_mine;
    }

    @Override
    public void initViews(View view) {
        Picasso.get().load(R.drawable.ic_head_default)
                .transform(new CircleTransform())
                .into(imgHead);
        shareUtils = new ShareUtils(context);
        userType = (int) SpUtils.get(context, Comm.SP_TYPE, 1);
    }

    @Override
    public void initDatas() {

    }

    @Override
    public void onResume() {
        super.onResume();
        String persion = (String) SpUtils.get(context, Comm.SP_PERION, "");
        if (userType == 1) {
            student = GsonUtils.str2Bean(persion, Student.class);
            txtHintId.setText("学号");
            txtHintName.setText("专业");
            if (TextUtils.isEmpty(student.getName())) {
                txtName.setText("未设置");
            } else {
                txtName.setText(student.getName());
            }
            txtId.setText(student.getStudentId());
            if (TextUtils.isEmpty(student.getMajor())) {
                txtMajor.setText("--");
            } else {
                txtMajor.setText(student.getMajor());
            }
            if (!TextUtils.isEmpty(student.getImgUrl())){
                Bitmap bitmap = ImageUtils.str2Bitmap(student.getImgUrl());
                Picasso.get().load(ImageUtils.bit2Uri(context,bitmap)).error(R.drawable.ic_head_default).transform(new CircleTransform()).into(imgHead);
            }
        } else {
            teacher = GsonUtils.str2Bean(persion, Teacher.class);
            txtHintId.setText("工号");
            txtHintName.setText("授课名称");
            if (TextUtils.isEmpty(teacher.getName())) {
                txtName.setText("未设置");
            } else {
                txtName.setText(teacher.getName());
            }
            txtId.setText(teacher.getTeachId());
            if (TextUtils.isEmpty(teacher.getCourse())) {
                txtMajor.setText("--");
            } else {
                txtMajor.setText(teacher.getCourse());
            }
            if (!TextUtils.isEmpty(teacher.getImgUrl())){
                Bitmap bitmap = ImageUtils.str2Bitmap(teacher.getImgUrl());
                Picasso.get().load(ImageUtils.bit2Uri(context,bitmap)).error(R.drawable.ic_head_default).transform(new CircleTransform()).into(imgHead);
            }
        }
    }

    @OnClick({R.id.btn_mine_exit, R.id.line_mine_about, R.id.line_mine_edit, R.id.line_mine_share, R.id.line_mine_advice})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_mine_exit:
                showDialog(1);
                break;
            case R.id.line_mine_advice:
                showDialog(2);
                break;
            case R.id.line_mine_about:
                showDialog(3);
                break;
            case R.id.line_mine_share:
                //调用原生qq分享
                shareUtils.shareUrl( "https://fir.im/1q3b");
                break;
            case R.id.line_mine_edit:
                ARouter.getInstance().build(Comm.ROUTE_EDIT_INFO).withInt("userType", userType).navigation();
                break;

        }
    }

    /**
     * 提示对话框
     */
    private void showDialog(int type) {
        String msg;
        String title;
        String right = "";
        String left;
        switch (type) {
            case 1:
                msg = "确定要退出登录吗？";
                right = "确定";
                left = "取消";
                title = "提示";
                break;
            case 2:
                msg = "请上线后再来吐槽";
                left = "知道了";
                title = "提示";
                break;
            default:
                msg = "考勤帮V" + BuildConfig.VERSION_NAME;
                left = "知道了";
                title = "关于App";
                break;
        }
        new MsgDialog.Builder(context)
                .setMsg(msg)
                .setBtnColor(ContextCompat.getColor(context, R.color.colorBlue))
                .setmTitle(title)
                .setTxtLeft(left, (dialog, which) -> {
                    dialog.dismiss();
                })
                .setTxtRight(right, (DialogInterface.OnClickListener) (dialog, which) -> {
                    SpUtils.put(context, Comm.SP_LOGIN, false);
                    ARouter.getInstance().build(Comm.ROUTE_LOGIN).navigation();
                    Objects.requireNonNull(getActivity()).finish();
                    dialog.dismiss();
                }).create().show();
    }
}
